import Vue from 'vue';
import axios from 'axios';
import { Message } from 'element-ui';
import { apiJava } from './root';

axios.defaults.timeout = 120000;

// axios.defaults.transformRequest = [function (data) {
//   if (Object.prototype.toString.call(data) != '[object FormData]' && Object.prototype.toString.call(data) != '[object String]') {
//     let src = ''
//     for (let item in data) {
//       src += encodeURIComponent(item) + '=' + encodeURIComponent(data[item] === null ? '' : data[item]) + '&'
//     }
//     return src;
//   } else {
//     return data;
//   }
// }]

let loading;
function startLoading() {
  loading = Vue.prototype.$loading({
    lock: true
  });
}
function endLoading() {
  loading.close();
}

//声明一个对象用于存储请求个数
let needLoadingRequestCount = 0;
function showFullScreenLoading() {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
}
function tryHideFullScreenLoading() {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
}

// 请求拦截,设置统一header
axios.interceptors.request.use(
  (config) => {
    let token = '';
    let moaToken = '';
    let AppKey = '';
    if (sessionStorage.getItem('userInfo')) {
      token = JSON.parse(sessionStorage.getItem('userInfo')).token;
      moaToken = JSON.parse(sessionStorage.getItem('userInfo')).moa_token;
      if (
        moaToken &&
        (config.url.indexOf('moa.yl1001.com') >= 0 ||
          config.url.indexOf('moatest.yl1001.com') >= 0)
      ) {
        config.headers['Authorization'] = moaToken;
        return config;
      }

      if (config.url.indexOf('upload.yl1001.com') < 0) {
        config.headers['Authorization'] = token;
        config.headers['Content-Type'] = 'application/json; charset=UTF-8';
      } else {
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
      }
    }

    if (sessionStorage.getItem('AppKey')) {
      AppKey = sessionStorage.getItem('AppKey');
    }

    if (process.env.NODE_ENV === 'development') {
      // token = "Bearer 40193ff0-ae9c-4c32-880f-fa6b7f809e18";
      if (apiJava == '/apiLocal') {
        config.headers['n-d-version'] =
          '{"yl1001-boot-career-report":"3.0","yl1001-boot-occupation-platform":"3.0","yl1001-boot-subject-database":"3.0"}'; // 龚祥
          // '{"yl1001-boot-career-report":"1.9","yl1001-boot-occupation-platform":"1.9","yl1001-boot-subject-database":"1.9"}'; //高峰
          // '{"yl1001-boot-career-report":"1.4","yl1001-boot-occupation-platform":"1.4","yl1001-boot-subject-database":"1.4","yl1001-boot-file":"1.4"}'; // 曾辉
      }
      AppKey = sessionStorage.getItem('AppKey'); // 一览职途后台key:f6a32cd6dd3bd220  // 深职大Key:3799d8973c277cc6
    }

    config.headers['Authorization'] = token;
    config.headers['AppKey'] = AppKey;
    return config;
  },
  (err) => {
    // tryHideFullScreenLoading();
    // Message.error({ message: '请求超时!' });
    return Promise.resolve(err);
  }
);

//  响应拦截
axios.interceptors.response.use(
  (res) => {
    // tryHideFullScreenLoading();
    if (parseInt(res.data.code) === 200) {
      return Promise.resolve(res.data);
    } else {
      return Promise.resolve(res.data);
    }
  },
  (err) => {
    // tryHideFullScreenLoading();

    return Promise.reject(err);
  }
);

let request = {
  get: function (url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, {
          params: params
        })
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  post: function (url, data) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  post1: function (url, data) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, data, { headers: { responseType: 'arraybuffer' } })
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  test: function (url, data) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
};

export default request;
